<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong_Proc extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->config("member", $this->config);

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );

	}
	function doit() {
		switch( $this->input->post("action") ) {
			case "new_member" :
				$this->_newMember();break;
			case "modify_member" :
				$this->_modifyMember();break;
			case "delete_member" :
				$this->_deleteMember();break;
			case "new_group" :
				$this->_newGroup();break;
			case "modify_group" :
				$this->_modifyGroup();break;
			case "delete_group" :
				$this->_deleteGroup();break;
			case "email" :
				$this->_email();break;
		}
	}
	
	function _newMember() {
		
		$this->load->model("admin/admin", $this->admin);
		
		$id			= $this->input->post("id");
		$email	= $this->input->post("email");
		$nick		= $this->input->post("nick");
		$password	= $this->admin->password( $this->input->post("password") );

		$contents	= $this->input->post("contents");
		
		$groups		= implode(",", $this->input->post("groups"));
		
		$dat = array(
			'id'		=> $id,
			'email'		=> $email,
			'nick'		=> $nick,
			'password'	=> $password,
			'groups'	=> $groups,
			'contents'	=> $contents
		);
		
		if ($this->config['form']['jumin']) {
			$dat['birth'] = $this->input->post("birth");
		}
		if ($this->config['form']['phone']) {
			$dat['phone'] = $this->input->post("phone0")."-".$this->input->post("phone1")."-".$this->input->post("phone2");
		}

		try {
			if ( $this->db->count("select count(*) from `wd_member` where `id`=?", array($id)) > 0 ) throw new Error("같은 ID가 존재합니다.");
			if ( $this->db->insert("wd_member", $dat) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}

		go($this->data->href."/".$this->data->kong->id);
	}
	
	function _modifyMember() {

		$this->load->model("admin/admin", $this->admin);
		
		$idx	= $this->input->post("idx");
		$id		= $this->input->post("id");
		$email	= $this->input->post("email");
		$nick	= $this->input->post("nick");
		
		$groups	= implode(",", $this->input->post("groups"));

		if ($this->input->post("options") !== "") {
			$options			= array();
			$options_list	= explode(",", $this->input->post("options"));
			foreach ($options_list as $v) {
				if ($v !== "") $options[$v] = $this->input->post($v);
			}
			$options			= serialize($options);
		}
				
		$contents = $this->input->post("contents");
		
		$dat = array(
			'id'		=> $id,
			'email'		=> $email,
			'nick'		=> $nick,
			'options'	=> $options,
			'groups'	=> $groups,
			'contents'	=> $contents
		);

		if ($this->config['form']['jumin']) {
			$dat['birth'] = $this->input->post("birth");
		}
		if ($this->config['form']['phone']) {
			$dat['phone'] = $this->input->post("phone0")."-".$this->input->post("phone1")."-".$this->input->post("phone2");
		}

		// password가 있을때!
		if ($this->input->post_exists("password")) $dat['password']	= $this->admin->password( $this->input->post("password") );

		try {
			if ( $this->db->count("select count(*) from `wd_member` where `id`=? and `idx`<>?", array($id, $idx)) > 0 ) throw new Error("같은 ID가 존재합니다.");
			if ( $this->db->update("wd_member", $dat, array("idx" => $idx)) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}
		
		go($this->data->href."/".$this->data->kong->id);

	}
	function _deleteMember() {

		$idx = $this->input->post("idx");

		try {
		
			$result = $this->db->result( "select `id` from `wd_member` where `idx`=?", array($idx) );

			if ( $this->db->delete("wd_member", array("idx" => $idx)) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}
		
		go($this->data->href."/".$this->data->kong->id);
		
	}
	function _newGroup() {
		
		$name = $this->input->post("name");
		
		$dat = array(
			'name'		=> $name
		);
		
		try {
			if ( $this->db->insert("wd_group", $dat) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}
		
		go($this->data->href."/".$this->data->kong->id."/group");
	}
	function _modifyGroup() {

		$idx = $this->input->post("idx");
		$name = $this->input->post("name");
		
		$dat = array(
			'name'		=> $name
		);

		try {
			if ( $this->db->update("wd_group", $dat, array("idx" => $idx)) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}
		
		go($this->data->href."/".$this->data->kong->id."/group");

	}
	function _deleteGroup() {

		try {
			if ( $this->db->delete("wd_group", array("idx" => $this->input->post("idx"))) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}
		
		go($this->data->href."/".$this->data->kong->id."/group");
		
	}
	function _email() {
		
		$this->load->helper("mail");
		
		$mail_list = array();		
		if ( $this->input->post("group_all") == 1 ) {
			$mail_list	= $this->db->fetch_assoc_one("select `email` from `wd_member`");
		}
		else {
			$group = $this->input->post("group");
			if (is_array($group)) {
				foreach($group as $v) {
					$mail_list = array_merge($mail_list, 
						$this->db->fetch_assoc_one("select `email` from `wd_member` where `groups` like '%{$v},%' or `groups` like '%,{$v}%' or `groups` = '{$v}'")
					);
				}
			}
		}

		if ( count($mail_list) == 0 ) alert("보낼 사람이 없습니다!");
				
		$writer		= $this->input->post("writer");
		$email		= $this->input->post("email");
		$subject	= $this->input->post("subject");
		$contents	= $this->input->post("contents");

		foreach($mail_list as $v) {
			mailto($writer,$email,$v,$subject,$contents);
		}

		go($this->data->href."/".$this->data->kong->id."/email");
	}
	// End
}